<template>
  <div class="goodsinfo-container">
      <!-- 商品轮播轮播图 -->
    <transition
    @before-enter="beforeEnter"
    @enter="enter"
    @after-enter="afterEnter">
    <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>

    <div class="mui-card">
        <div class="mui-card-content">
            <div class="mui-card-content-inner">
                <swiper :lunboList="lunboList" :isfull="false"></swiper>
            </div>
        </div>
    </div>
    <!-- 商品购买区域 -->
    <div class="mui-card">
	    <div class="mui-card-header">商品名称</div>
		<div class="mui-card-content">
			<div class="mui-card-content-inner">
					<p class="price">
                        <span class="now_price">销售价:￥{{ info.sell_price }}</span>
                        <span class="old_price">市场价:￥{{ info.market_price }}</span>                       
                    </p>
                    <p>购买数量:<numbox @getCount="getSelectedCount" :max="info.stock_quantity"></numbox></p>
                    <p>
                        <mt-button type="primary" size="small">立即购买</mt-button>
                        <mt-button type="danger" size="small" @click="addCar">加入购物车</mt-button>
                    </p>
			</div>
		</div>
	</div>

    <!-- 商品参数 -->
    <div class="mui-card">
        <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品货号：{{ info.goods_no }}</p>
                    <p>库存情况：{{ info.stock_quantity }}件</p>
                    <p>上架时间：{{ info.add_time | dateFormat }}</p>
                </div>
            </div>
        <div class="mui-card-footer">
            <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
            <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
        </div>
</div>
			
  </div>
</template>

<script>
import swiper from '../subcomponents/swiper'  //导入轮播图组件
import numbox from '../subcomponents/numbox'  //导入商品数量选着组件
export default {
    data(){
        return {
            id: this.$route.params.id,  //获取路由id
            lunboList:[],  //轮播图数据
            info:{},   //商品信息
            ballFlag : false,          
            selectedCount: 1  //选择数量默认等于1
        }
    },
    methods:{
        //获取轮播图数据
        getlunboList(){
            this.$http.get('api/getthumimages/' + this.id).then(result => {
                if(result.body.status === 0){
                    result.body.message.forEach(item => {
                        item.img = item.src
                    });
                    this.lunboList = result.body.message
                }
            })
        },
        //获取商品信息
        getgoodsInfo(){
            this.$http.get('api/goods/getinfo/' + this.id).then(result => {
                if(result.body.status === 0){
                    this.info = result.body.message[0]
                }
            })
        },
        //跳转到描述页面
        goDesc(id) {
        // 点击使用编程式导航跳转到 图文介绍页面
            this.$router.push({ name: "goodsdesc", params: { id } });
            console.log(123)
        },
        // 点击跳转到 评论页面
        goComment(id) {        
            this.$router.push({ name: "goodscomment", params: { id } });
            console.log(333)
        },
        //点击购买加入购物车
        addCar(){       
            this.ballFlag = !this.ballFlag
            // 手动拼接出要保存到store中的商品信息对象
            var goodsinfo = {
                id : this.id,
                price : this.info.sell_price,
                count : this.selectedCount,
                selected : true
            }
            this.$store.commit('addToCar',goodsinfo)
        },
        //小球动画的钩子函数
         beforeEnter(el) {
            el.style.transform = "translate(0, 0)";
         },
         enter(el, done) {
            el.offsetWidth;

            // 获取小球的 在页面中的位置
            const ballPosition = this.$refs.ball.getBoundingClientRect();
            // 获取 徽标 在页面中的位置
            const badgePosition = document
                .getElementById("badge")
                .getBoundingClientRect();

            const x = badgePosition.left - ballPosition.left;
            const y = badgePosition.top - ballPosition.top;

            el.style.transform = `translate(${x}px, ${y}px)`;
            el.style.transition = "all .5s cubic-bezier(.4,-0.3,1,.68)";
            //动画执行结束立马调用done（）
            
      done();
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag;
      this.flag = true
    },
    //获取子组件中选择商品的数量
    getSelectedCount(count){
        this.selectedCount = count
        console.log("父组件拿到的数量值为： " + this.selectedCount);
    }

    },
    created(){
        this.getlunboList()
        this.getgoodsInfo()
    },
    components:{
        swiper,
        numbox
    }
}
</script>

<style lang="scss" scoped>
.goodsinfo-container {
  background-color: #eee;
  overflow: hidden;
  .now_price {
    color: red;
    font-size: 16px;
    font-weight: bold;
  }
  .old_price {
      font-size: 13px;
      text-decoration: line-through;
  }
  .mui-card-footer {
    display: block;
    button {
      margin: 15px 0;
    }
  }

  .ball{
      width: 15px;
      height: 15px;
      background-color: red;
      border-radius: 50%;
      position: absolute;
      z-index: 99;
      top: 390px;
      left: 146px;
  }
}
</style>
